package res

import "net/http"

type R[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data"`
}

// 构造函数
func NewR[T any]() *R[T] {
	return &R[T]{
		Code: http.StatusOK,
		Msg:  "ok",
	}
}

// 快捷方法
func Ok[T any](data T) *R[T] {
	return &R[T]{
		Code: http.StatusOK,
		Msg:  "ok",
		Data: data,
	}
}

func Fail[T any](msg string, data T) *R[T] {
	return &R[T]{
		Code: http.StatusBadRequest,
		Msg:  msg,
		Data: data,
	}
}

func Error[T any](msg string, data T) *R[T] {
	return &R[T]{
		Code: http.StatusInternalServerError,
		Msg:  msg,
		Data: data,
	}
}

// 原有的链式方法保持不变
func (hrep *R[T]) Ok(data T) *R[T] {
	hrep.Code = http.StatusOK
	hrep.Msg = "ok"
	hrep.Data = data
	return hrep
}

func (hrep *R[T]) Fail(data T) *R[T] {
	hrep.Code = http.StatusBadRequest
	hrep.Msg = "坏请求"
	hrep.Data = data
	return hrep
}

func (hrep *R[T]) Err(data T) *R[T] {
	hrep.Code = http.StatusInternalServerError
	hrep.Msg = "服务器错误"
	hrep.Data = data
	return hrep
}

func (hrep *R[T]) SetData(data T) *R[T] {
	hrep.Data = data
	return hrep
}

func (hrep *R[T]) SetCode(code int) *R[T] {
	hrep.Code = code
	return hrep
}

func (hrep *R[T]) SetMsg(msg string) *R[T] {
	hrep.Msg = msg
	return hrep
}
